<template>
  <div class="main-content">
    <!-- 列表页 -->
    <div v-if="showFlag">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-row :gutter="20" class="slt">
          <el-form-item :label='职业类型'>
            <el-input prefix-icon="el-icon-search" v-model="searchForm.zhiyeleixing" placeholder="职业类型"
                      clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="success" @click="search()">查询</el-button>

          </el-form-item>
        </el-row>

        <el-row class="ad">
          <el-form-item>
            <el-button
                type="success"
                icon="el-icon-plus"
                @click="addOrUpdateHandler()"
            >新增
            </el-button>
            <el-button
                :disabled="dataListSelections.length <= 0"
                type="danger"
                icon="el-icon-delete"
                @click="deleteHandler()"
            >删除
            </el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <div class="table-content">
        <el-table
            class="tables"
            :data="dataList"
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandler"
        >
          <el-table-column type="selection" align="center" width="50"></el-table-column>
          <el-table-column label="索引" type="index" width="50" align="center"></el-table-column>
          <el-table-column align="center" prop="zhiyeleixing" header-align="center" label="职业类型">
            <template slot-scope="scope">
              {{ scope.row.zhiyeleixing }}
            </template>
          </el-table-column>
          <el-table-column width="300" align="center" header-align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="success" icon="el-icon-view" size="mini"
                         @click="addOrUpdateHandler(scope.row.id,'info')">详情
              </el-button>
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="addOrUpdateHandler(scope.row.id)">修改
              </el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteHandler(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            clsss="pages"
            :layout="layouts"
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            :total="totalPage"
            class="pagination-content"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
    <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>

  </div>
</template>
<script>
import axios from 'axios'
import AddOrUpdate from "./add-or-update";

export default {
  data() {
    return {
      searchForm: {
        key: ""
      },
      form: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag: false,
      layouts: 'total, sizes, prev, pager, next, jumper',

    };
  },
  created() {
    this.getDataList();
  },
  mounted() {

  },

  components: {
    AddOrUpdate,
  },
  methods: {

    init() {
    },
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
      }
      if (this.searchForm.zhiyeleixing !== '' && this.searchForm.zhiyeleixing !== undefined) {
        params['zhiyeleixing'] = '%' + this.searchForm.zhiyeleixing + '%'
      }
      this.$http({
        url: "zhiyeleixing/page",
        method: "get",
        params: params
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id, type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if (type !== 'info') {
        type = 'else';
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type);
      });
    },
    // 查看评论
    // 下载
    download(file) {
      window.open(`${file}`)
    },
    // 删除
    deleteHandler(id) {
      var ids = id
          ? [Number(id)]
          : this.dataListSelections.map(item => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "zhiyeleixing/delete",
          method: "post",
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.search();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },


  }

};
</script>
<style lang="scss" scoped>
.main-content {
  .search-form {
    margin-bottom: 15px;
    padding: 15px;
    border-radius: 4px;
    background-color: #fff;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);

    .el-form-item {
      margin-bottom: 15px;
    }

    /deep/ .el-button--success {
      height: 40px;
      color: #ffffff;
      font-size: 14px;
      border-width: 0px;
      border-radius: 4px;
      background-color: #67c23a;
    }

    /deep/ .ad .el-button--success {
      height: 40px;
      color: #ffffff;
      font-size: 14px;
      border-width: 0px;
      border-radius: 4px;
      background-color: #67c23a;
    }

    /deep/ .ad .el-button--danger {
      height: 40px;
      color: #ffffff;
      font-size: 14px;
      border-width: 0px;
      border-radius: 4px;
      background-color: #f56c6c;
    }
  }

  .table-content {
    margin-top: 15px;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
    background-color: #ffffff;

    .el-table {
      border: 1px solid #ebeef5;
    }
  }

  .el-pagination {
    margin-top: 20px;
    text-align: center;
    display: flex;
    justify-content: center;
  }

  .tables {
    & /deep/ .el-table__body tr {
      background-color: #ffffff !important;
      color: #606266 !important;
    }

    & /deep/ .el-table__body tr.el-table__row--striped td {
      background: #fafafa;
    }

    & /deep/ .el-table__body tr.el-table__row--striped {
      background-color: #fafafa !important;
      color: #606266 !important;
    }

    & /deep/ .el-table__body tr:hover > td {
      background-color: #f5f7fa !important;
      color: #606266 !important;
    }

    & /deep/ .el-button--success {
      height: 32px;
      color: #ffffff;
      font-size: 12px;
      border-width: 0px;
      border-radius: 4px;
      background-color: #409eff;
      padding: 0 16px;
      margin: 3px;
    }

    & /deep/ .el-button--primary {
      height: 32px;
      color: #ffffff;
      font-size: 12px;
      border-width: 0px;
      border-radius: 4px;
      background-color: #409eff;
      padding: 0 16px;
      margin: 3px;
    }

    & /deep/ .el-button--danger {
      height: 32px;
      color: #ffffff;
      font-size: 12px;
      border-width: 0px;
      border-radius: 4px;
      background-color: #f56c6c;
      padding: 0 16px;
      margin: 3px;
    }

    & /deep/ .el-button {
      margin: 3px;
      min-width: 70px;
    }

    & /deep/ .el-table-column--operation .cell {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
  }
}

.slt {
  display: flex;
  flex-wrap: wrap;
  margin: 0 !important;
}

.ad {
  display: flex;
  margin: 0 !important;
}

/deep/ .el-table th {
  background-color: #f5f7fa !important;
  color: #606266 !important;
  font-weight: bold;
}

/deep/ .el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/deep/ .el-table th.is-leaf {
  border-bottom: 1px solid #ebeef5;
}

/deep/ .el-table,
/deep/ .el-table tr,
/deep/ .el-table__body {
  background-color: #ffffff !important;
}

/deep/ .el-table__fixed-right::before,
/deep/ .el-table__fixed::before {
  background-color: #ebeef5;
}

.table-img {
  width: 100px;
  height: 70px;
  object-fit: cover;
  border-radius: 4px;
}
</style>
